package com.xudu.tourbackend.controller.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cloopen.rest.sdk.BodyType;
import com.cloopen.rest.sdk.CCPRestSmsSDK;
import com.xudu.tourbackend.common.DeleteBatchRequest;
import com.xudu.tourbackend.common.Result;
import com.xudu.tourbackend.common.globalEnum.ErrorCode;
import com.xudu.tourbackend.controller.UserController;
import com.xudu.tourbackend.entity.User;
import com.xudu.tourbackend.entity.request.LoginRequest;
import com.xudu.tourbackend.entity.request.RegisterRequest;
import com.xudu.tourbackend.entity.request.SearchUserRequest;
import com.xudu.tourbackend.entity.request.UpdateUserRequest;
import com.xudu.tourbackend.entity.vo.UserVo;
import com.xudu.tourbackend.exception.ServiceException;
import com.xudu.tourbackend.mapper.UserMapper;
import com.xudu.tourbackend.service.IUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @className: UserControllerImpl
 * @description: TODO
 * @author: xudu
 * @create: 2024-06-17
 */
@CrossOrigin(origins = "*")
@RestController
public class UserControllerImpl implements UserController {

    @Resource
    private UserMapper userMapper;

    @Resource
    private IUserService userService;


    @Override
    public Result userRegister(@RequestBody RegisterRequest registerRequest) throws Exception {
        String userAccount = registerRequest.userAccount;
        String userPassword = registerRequest.userPassword;
        String checkUserPassword = registerRequest.checkUserPassword;
        if (StringUtils.isAnyBlank(userAccount, userPassword, checkUserPassword)) {
            throw new ServiceException(ErrorCode.NULL_ERROR);
        }

        int i = userService.register(userAccount, userPassword, checkUserPassword);
        return Result.success(i);
    }

    @Override
    public Result userRegisterByPhone(@RequestBody RegisterRequest registerRequest) {
        String userPhone = registerRequest.userPhone;
        if (StringUtils.isBlank(userPhone)){
            throw new ServiceException(ErrorCode.NULL_ERROR);
        }
        String string = userService.registerByPhone(userPhone);
        return Result.success(string);
    }

    @Override
    public Result registerVerifyCode(String phone, String code) {
        if (StringUtils.isAnyBlank(phone, code)){
            throw new ServiceException(ErrorCode.NULL_ERROR);
        }
        int i = userService.verifyCode(phone, code);
        return Result.success(i);
    }

    @Override
    public Result login(@RequestBody LoginRequest loginRequest, HttpServletRequest request) throws Exception {
        String userAccount = loginRequest.userAccount;
        String userPassword = loginRequest.userPassword;
        if (StringUtils.isAnyBlank(userAccount, userPassword)){
            throw new ServiceException(ErrorCode.NULL_ERROR);
        }
        UserVo loginUser = userService.login(userAccount, userPassword, request);
        return Result.success(loginUser);
    }

    @Override
    public Result loginByPhone(@RequestBody LoginRequest loginRequest) {
        String userPhone = loginRequest.userPhone;
        if (StringUtils.isBlank(userPhone)){
            throw new ServiceException(ErrorCode.NULL_ERROR);
        }
        String string = userService.loginByPhone(userPhone);
        return Result.success(string);

    }

    @Override
    public Result loginVerifyCode(@RequestBody LoginRequest loginRequest, HttpServletRequest request) {
        String userPhone = loginRequest.userPhone;
        String code = loginRequest.code;
        if (StringUtils.isAnyBlank(userPhone, code)){
            throw new ServiceException(ErrorCode.NULL_ERROR);
        }
        UserVo userVo = userService.loginVerifyCode(userPhone, code, request);
        return Result.success(userVo);
    }

    @Override
    public Result updateUser(UpdateUserRequest updateUserRequest, HttpServletRequest request) throws Exception {
        Boolean b = userService.updateUser(updateUserRequest, request);
        return Result.success(b);
    }

    @Override
    public Result searchUser(SearchUserRequest searchUserRequest, HttpServletRequest request) {
        if (searchUserRequest == null){
            throw new ServiceException(ErrorCode.NULL_ERROR);
        }
        IPage<UserVo> userVoIPage = userService.searchUser(searchUserRequest, request);
        return Result.success(userVoIPage);
    }

    @Override
    public Result deleteUser(DeleteBatchRequest deleteBatchRequest, HttpServletRequest request) {
        if (deleteBatchRequest == null){
            throw new ServiceException(ErrorCode.NULL_ERROR);
        }
        int i = userService.deleteUser(deleteBatchRequest.getIds(), request);
        return Result.success(i);

    }
}
